


//
//  YDShopMapViewController.m
//  YaDiuDiu
//
//  Created by YaoYang on 2017/10/28.
//  Copyright © 2017年 jy. All rights reserved.
//

#import "YDShopMapViewController.h"
#import <MAMapKit/MAMapKit.h>
#import <AMapFoundationKit/AMapFoundationKit.h>


@interface YDShopMapViewController ()<MAMapViewDelegate,MAMultiPointOverlayRendererDelegate>
@property (nonatomic,strong) MAMapView *mapView;

@end

@implementation YDShopMapViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self showNav:@"地图"];

    
    
    [AMapServices sharedServices].apiKey =@"cd5594a27590cc34cdb6a76e802c6dd7";
    
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    ///地图需要v4.5.0及以上版本才必须要打开此选项（v4.5.0以下版本，需要手动配置info.plist）
    [AMapServices sharedServices].enableHTTPS = YES;
    
    ///初始化地图
    _mapView = [[MAMapView alloc] initWithFrame:CGRectMake(0, 64, ScreenWidth, ScreenHeight-64)];
    ///如果您需要进入地图就显示定位小蓝点，则需要下面两行代码
    _mapView.showsUserLocation = YES;
    _mapView.userTrackingMode = MAUserTrackingModeFollow;
    ///把地图添加至view
    [self.view addSubview:_mapView];
    
}


- (void)sendLocationWithArray:(NSArray *)arrLocation{
    NSMutableArray *arrLo = [NSMutableArray array];
    for (NSDictionary *dic in arrLocation) {
    NSArray *arr = @[dic[@"longitude"],dic[@"latitude"]];
    [arrLo addObject:arr];
    }
    
    
    ///根据file读取出经纬度数组
//    NSString *file = [[NSBundle mainBundle] pathForResource:@"10w" ofType:@"txt"];
//    NSString *locationString = [NSString stringWithContentsOfFile:file encoding:NSUTF8StringEncoding error:nil];
//    NSArray *locations = [locationString componentsSeparatedByString:@"\n"];
    
    ///创建MultiPointItems数组，并更新数据
    
    NSMutableArray *items = [NSMutableArray array];

    for (int i = 0; i < arrLo.count; ++i)
    {
        @autoreleasepool {
            
            MAMultiPointItem *item = [[MAMultiPointItem alloc] init];
            
            NSArray *coordinate = arrLo[i];//[arrLo[i] componentsSeparatedByString:@","];
            
            if (coordinate.count == 2)
            {
                item.coordinate = CLLocationCoordinate2DMake([coordinate[1] floatValue], [coordinate[0] floatValue]);
                
                [items addObject:item];
            }
        }
    }
    
    ///根据items创建海量点Overlay MultiPointOverlay
    MAMultiPointOverlay *_overlay = [[MAMultiPointOverlay alloc] initWithMultiPointItems:items];
    
    ///把Overlay添加进mapView
    [self.mapView addOverlay:_overlay];
}


- (MAOverlayRenderer *)mapView:(MAMapView *)mapView rendererForOverlay:(id <MAOverlay>)overlay
{
    if ([overlay isKindOfClass:[MAMultiPointOverlay class]])
    {
        MAMultiPointOverlayRenderer * renderer = [[MAMultiPointOverlayRenderer alloc] initWithMultiPointOverlay:overlay];
        
        ///设置图片
        renderer.icon = [UIImage imageNamed:@"果"];
        ///设置锚点
        renderer.anchor = CGPointMake(0.5, 1.0);
        renderer.delegate = self;
        return renderer;
    }
    
    return nil;
}
- (void)multiPointOverlayRenderer:(MAMultiPointOverlayRenderer *)renderer didItemTapped:(MAMultiPointItem *)item
{
    NSLog(@"item :%@ <%f, %f>", item, item.coordinate.latitude, item.coordinate.longitude);
}


- (void)clickBack{
    [self.navigationController popViewControllerAnimated:YES];
}

@end
